<?php
namespace app\index\service;


use app\index\model\ContractCaiListModel;
use app\index\model\EnginBudbianModel;
use app\index\model\EnginBudgetListModel;
use app\index\model\EnginBudgetModel;
use app\index\model\EnginBudshouModel;
use app\index\model\EnginBudsubModel;
use think\Db;

class BudgetService extends CommonService
{
    public function get_budsub_nav(){
        return [
            'cai' => '材料费',
            'ren' => '人工费',
            'ji' => '机械费',
            'fen' => '分包费',
            'cuo' => '措施费',
            'jian' => '管理费',
        ];
    }

    public function get_budshou_nav(){
        return [
            'engin' => '项目收入',
            'qi' => '其他收入',
        ];
    }

    public function create_engin_budget_number(){
        $tou = 'CBYS';
        $daystr = substr(date("Ymd"), 2);
        $number = EnginBudgetModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }

    public function get_budsub_all($pid=0){
        $data = EnginBudsubModel::field('*,id as value')
            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid])
            ->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['children'] = $this->get_budsub_all($item['id']);
        }
        return $data;
    }

    public function get_budshou_all($pid=0){
        $data = EnginBudshouModel::field('*,id as value')
            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid])
            ->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['children'] = $this->get_budshou_all($item['id']);
        }
        return $data;
    }

    public function get_budsubquan_all($pid=0, $opname=''){
        $data = EnginBudsubModel::field('id,name,name as title')
            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid])
            ->where('name','neq','公司费用')
            ->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            if(empty($opname)){
                $item['opname'] = $item['name'];
            }else{
                $item['opname'] = $opname.'/'.$item['name'];
            }

            $item['spread'] = true;
            $item['children'] = $this->get_budsubquan_all($item['id'], $item['opname']);
        }
        return $data;
    }

//    public function get_budsubquan_show_list($pid=0, $budid){
//        $data = EnginBudsubModel::field('id,name,name as title')
//            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid])
//            ->order('sort ASC')->select()->toArray();
//        foreach ($data as &$item){
//            $children = $this->get_budsubquan_show_list($item['id'], $budid);
//            if(!$children){
//                $children = EnginBudgetListModel::where(['budid'=>$budid,'subid'=>$item['id']])->select()->toArray();
//                foreach ($children as &$vv){
//                    $vv['subid_name'] = $vv['name'];
//                    $vv['name'] = '#';
//                    $vv['zhannum'] = ContractCaiListModel::alias('list')
//                        ->join('contract_cai cai','cai.id = list.caiid')
//                        ->where(['cai.deleted_user'=>0,'cai.isturn'=>1])
//                        ->where('cai.status','in',[0,1])
//                        ->where(['list.listid'=>$vv['id']])
//                        ->sum('list.num');
//                    $vv['shengnum'] = $vv['num'] - $vv['zhannum'];
//                }
//            }
//
//            $item['children'] = $children;
//        }
//        return $data;
//    }



    public function get_shangji_dingji_data($id=0, $tongdata){
        $arr=[];
        $status = true;
        while ($status) {
            $info = EnginBudsubModel::where(['id'=>$id])->find()->toArray();
            if($info){
                $info['money'] = $tongdata['money'];
                $info['zhanmoney'] = $tongdata['zhanmoney'];
                $info['shengmoney'] = $tongdata['shengmoney'];

                $arr[$id] = $info;
                if($info['pid'] > 0){
                    $id = $info['pid'];
                }else{
                    $status = false;
                }
            }else{
                $status = false;
            }
        }

        return $arr;
    }

    public function get_shangji_dingji($id=0){
        $arr=[];
        $status = true;
        while ($status) {
            $info = EnginBudsubModel::where(['id'=>$id])->find();
            if($info){
                $arr[] = $info['id'];
                if($info['pid'] > 0){
                    $id = $info['pid'];
                }else{
                    $status = false;
                }
            }else{
                $status = false;
            }
        }

        return $arr;
    }

    public function get_engin_budsub_all_xiala($pid=0, $nogong, $value=''){
        $wherenogong = [];
        if(!empty($nogong)){
            $wherenogong[] = ['name','neq',$nogong];
        }


        $data = EnginBudsubModel::field('id,name,explain')
            ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'status'=>1,'pid'=>$pid])
            ->where($wherenogong)
            ->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            if(empty($value)){
                $item['value'] = $item['name'];
            }else{
                $item['value'] = $value.'/'.$item['name'];
            }
            $item['children'] = $this->get_engin_budsub_all_xiala($item['id'], $nogong, $item['value']);
        }

        return $data;
    }

    public function get_engin_budsub_guan_all_xiala_sea($pid=0){
        $wheress = [];
        if($pid == 0){
            $wheress[] = ['name', '=', '项目费用'];
        }
        $data = EnginBudsubModel::field('id as value,name as title,name')
            ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0,'pid'=>$pid])
            ->where($wheress)->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['children'] = $this->get_engin_budsub_guan_all_xiala_sea($item['value']);
        }

        return $data;
    }


    public function get_engin_budsub_guan_all_xiala($pid=0, $value=''){
        $wheress = [];
        if($pid == 0){
            $wheress[] = ['name', '=', '项目费用'];
        }
        $data = EnginBudsubModel::field('id,name')
            ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0,'pid'=>$pid])
            ->where($wheress)->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            if(empty($value)){
                $item['value'] = $item['name'];
            }else{
                $item['value'] = $value.'/'.$item['name'];
            }
            $item['children'] = $this->get_engin_budsub_guan_all_xiala($item['id'], $item['value']);
        }

        return $data;
    }


    public function get_engin_budsub_guan_gong_all_xiala_sea($pid=0){
        $wheress = [];
        if($pid == 0){
            $wheress[] = ['name', '=', '公司费用'];
        }
        $data = EnginBudsubModel::field('id as value,name as title,name')
            ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0,'pid'=>$pid])
            ->where($wheress)->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            $item['children'] = $this->get_engin_budsub_guan_gong_all_xiala_sea($item['value']);
        }

        return $data;
    }
    public function get_engin_budsub_guan_gong_all_xiala($pid=0, $value=''){
        $wheress = [];
        if($pid == 0){
            $wheress[] = ['name', '=', '公司费用'];
        }
        $data = EnginBudsubModel::field('id,name')
            ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0,'pid'=>$pid])
            ->where($wheress)->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            if(empty($value)){
                $item['value'] = $item['name'];
            }else{
                $item['value'] = $value.'/'.$item['name'];
            }
            $item['children'] = $this->get_engin_budsub_guan_gong_all_xiala($item['id'], $item['value']);
        }

        return $data;
    }



    public function get_engin_budshou_engin_all_xiala($pid=0, $value=''){
        $wheress = [];
        if($pid == 0){
            $wheress[] = ['name', '=', '项目收入'];
        }
        $data = EnginBudshouModel::field('id,name')
            ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0,'pid'=>$pid])
            ->where($wheress)->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            if(empty($value)){
                $item['value'] = $item['name'];
            }else{
                $item['value'] = $value.'/'.$item['name'];
            }
            $item['children'] = $this->get_engin_budshou_engin_all_xiala($item['id'], $item['value']);
        }

        return $data;
    }


    public function get_engin_budshou_qi_all_xiala($pid=0, $value=''){
        $wheress = [];
        if($pid == 0){
            $wheress[] = ['name', '=', '其他收入'];
        }
        $data = EnginBudshouModel::field('id,name')
            ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0,'pid'=>$pid])
            ->where($wheress)->order('sort ASC')->select()->toArray();
        foreach ($data as &$item){
            if(empty($value)){
                $item['value'] = $item['name'];
            }else{
                $item['value'] = $value.'/'.$item['name'];
            }
            $item['children'] = $this->get_engin_budshou_qi_all_xiala($item['id'], $item['value']);
        }

        return $data;
    }


    public function check_budsub_name($name, $topid=0, $id=0){
        return !!EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0])
            ->where(Db::raw("topid=".$topid.' or id='.$topid))->where('id','neq', $id)->find();
    }

    public function check_budshou_name($name, $topid=0, $id=0){
        return !!EnginBudshouModel::where(['comid'=>$this->userinfo['comid'],'name'=>$name,'deleted_user'=>0])
            ->where(Db::raw("topid=".$topid.' or id='.$topid))->where('id','neq', $id)->find();
    }


    public function get_subid_quanname($subid){
        $str = '';
        while ($subid > 0) {
            $subinfo = EnginBudsubModel::where(['id'=>$subid])->find();
            if($subinfo){
                if(empty($str)){
                    $str = $subinfo['name'];
                }else{
                    $str = $subinfo['name'].'/'.$str;
                }
                $subid = $subinfo['pid'];
            }
        }

        return $str;
    }

    public function get_budid_quanname($subid){
        $str = '';
        while ($subid > 0) {
            $subinfo = EnginBudshouModel::where(['id'=>$subid])->find();
            if($subinfo){
                if(empty($str)){
                    $str = $subinfo['name'];
                }else{
                    $str = $subinfo['name'].'/'.$str;
                }
                $subid = $subinfo['pid'];
            }else{
                $subid = 0;
            }
        }

        return $str;
    }







    public function get_budshou_tops($id){
        $arr = [];

        $status = true;
        while ($status) {
            $info = EnginBudshouModel::where(['id'=>$id])->find();
            if($info){
                $arr[] = $id;
                if($info['pid'] > 0){
                    $id = $info['pid'];
                }else{
                    $status = false;
                }
            }else{
                $status = false;
            }
        }

        return $arr;
    }


    public function get_budshou_sons($id){
        $list = [$id];
        $seaarr = [$id];

        while (count($seaarr) > 0) {
            $son = EnginBudshouModel::where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0])
                ->where('pid','in',$seaarr)->column('id');

            $seaarr = [];
            foreach ($son as $v){
                $list[] = $v;
                $seaarr[] = $v;
            }
        }
        return $list;
    }

    public function get_budsub_sonsss($id){
        $list = [$id];
        $seaarr = [$id];

        while (count($seaarr) > 0) {
            $son = EnginBudsubModel::where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0])
                ->where('pid','in',$seaarr)->column('id');

            $seaarr = [];
            foreach ($son as $v){
                $list[] = $v;
                $seaarr[] = $v;
            }
        }
        return $list;
    }


    public function get_budsub_tops($id){
        $arr = [];

        $status = true;
        while ($status) {
            $info = EnginBudsubModel::where(['id'=>$id])->find();
            if($info){
                $arr[] = $id;
                if($info['pid'] > 0){
                    $id = $info['pid'];
                }else{
                    $status = false;
                }
            }else{
                $status = false;
            }
        }

        return $arr;
    }

    public function get_budsub_sons($id){
        static $arr = [];
        $list = EnginBudsubModel::where(['pid'=>$id])
            ->where(['comid'=>$this->userinfo['comid'],'status'=>1,'deleted_user'=>0])
            ->column('id');
        foreach($list as $val){
            $arr[] = $val;
            $this->get_budshou_sons($val);
        }

        return $arr;
    }




    public function create_engin_budbian_number(){
        $tou = 'YSBG';
        $daystr = substr(date("Ymd"), 2);
        $number = EnginBudbianModel::where('number','like','%'.$tou.$daystr.'%')->order('number DESC')->value('number');
        if(!$number){
            $str = $tou.$daystr.'00001';
        }else{
            $str = $tou.((int)str_replace($tou,'',$number)+1);
        }
        return $str;
    }


    public function get_budshou_pid_by_name($topname, $name=''){
        $topid = EnginBudshouModel::where(['comid'=>$this->userinfo['comid'],'pid'=>0,'topid'=>0,'name'=>$topname])->value('id');
        $kemuid = EnginBudshouModel::where(['topid'=>$topid,'name'=>$name])->value('id');
        return $kemuid;
    }

    public function get_budshou_son($topname, $name=''){
        $topid = EnginBudshouModel::where(['comid'=>$this->userinfo['comid'],'pid'=>0,'topid'=>0,'name'=>$topname])->value('id');
        $kemuid = EnginBudshouModel::where(['topid'=>$topid,'name'=>$name])->value('id');
        return $kemuid;
    }






}
